package net.jiastudy.web.rest.controller.project;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.jiastudy.biz.project.entity.PersonalProjectCategory;
import net.jiastudy.biz.project.service.PersonalProjectCategoryService;
import net.jiastudy.biz.user.entity.User;
import net.jiastudy.core.base.BaseController;
import net.jiastudy.core.common.validgroup.Create;
import net.jiastudy.core.common.validgroup.Update;

@RestController
@RequestMapping("/category")
@Api(value = "CategoryController  个人项目类型")
public class PersonalProjectCategoryController extends BaseController {
	@Resource
	private PersonalProjectCategoryService categoryService;
	
	@RequestMapping(path = "/checkcode", method = RequestMethod.GET)
	@ApiOperation(value = "检查编码是否重复", notes = "根据code检查是否重复,true为可用，false为不可用")
	public boolean checkCode(@RequestParam(required = true) String code, Long id) {
		return !categoryService.checkCodeExists(code, id);
	}
	
	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation(value = "新增", notes = "")
	public PersonalProjectCategory create(@Validated({ Create.class }) @RequestBody PersonalProjectCategory category) {
		User user = this.getCurrentUser();
		category.setCreatedUserId(user.getId());
		return categoryService.createCategory(category);
	}

	@RequestMapping(path = "/{id}", method = RequestMethod.PUT)
	@ApiOperation(value = "编辑")
	public PersonalProjectCategory edit(@PathVariable @ApiParam(name = "id", value = "类型ID", required = true) Long id,
			@Validated({ Update.class }) @RequestBody PersonalProjectCategory category) {
		category.setId(id);
		return categoryService.updateCategory(category);
	}
	
	@RequestMapping(method = RequestMethod.GET)
	@ApiOperation(value = "获取列表")
	public List<PersonalProjectCategory> finds(String keywords) {
		return categoryService.findCategorys(keywords);
	}
}
